package com.jnu.book;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

public class EditBookActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.input_item);

        Intent intent =getIntent();
        int position = intent.getIntExtra("position",0);
        EditText editTextName =findViewById(R.id.editText_newBook);
        String name=intent.getStringExtra("name");

        if(null!=name){
            editTextName.setText(name);
        }

        Button buttonOk =this.findViewById(R.id.button_ok);
        buttonOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();

                intent.putExtra("position",position);
                intent.putExtra("name",editTextName.getText().toString());

                setResult(BookListFragment.RESULT_CODE_ADD_DATA,intent);
                EditBookActivity.this.finish();
            }
        });
        Button buttonCancel=this.findViewById(R.id.button_cancel);
        buttonCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditBookActivity.this.finish();
            }
        });
    }
}

